﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Core;
using PowerPoint;
using WPSPropertyPlugin.Objects;
using WPSPropertyPlugin.Shapes;

namespace WPSPropertyPlugin
{
  public partial class frmProperty : Form
  {
    private WPSApplication WPSApplication { get; set; }

    private object selectionObjectId = null;

    public frmProperty(WPSApplication application)
    {
      WPSApplication = application;
      WPSApplication.OnWordSelectionChange += WPSApplication_OnWordSelectionChange;
      WPSApplication.OnPowerPointSelectionChange += WPSApplication_OnPowerPointSelectionChange;
      WPSApplication.OnExcelSelectionChange += WPSApplication_OnExcelSelectionChange;

      InitializeComponent();
    }

    private void WPSApplication_OnExcelSelectionChange(WPSExcelApplicationEventArgs obj)
    {
      // Todo...
    }

    private void WPSApplication_OnPowerPointSelectionChange(WPSPowerPointApplicationEventArgs args)
    {
      this.UpdatePowerPointSelectionInfo(args);
    }

    private void WPSApplication_OnWordSelectionChange(WPSWordApplicationEventArgs args)
    {
      this.UpdateWordSelectionInfo(args);
    }


    private void SetShapeProperty(IOfficeShape shapeObject)
    {
      if (shapeObject == null || WPSApplication == null)
      {
        return;
      }
      this.propertyPane.SelectedObject = new ShapeProperty(shapeObject);
    }

    public void UpdateOfficeShapeInfo(IOfficeShape shapeObject)
    {
      if (shapeObject != null)
      {
        var objId = shapeObject.Guid;
        if (!object.Equals(objId, selectionObjectId))
        {
          selectionObjectId = objId;
          SetShapeProperty(shapeObject);
        }
      }
      else
      {
        selectionObjectId = null;
        this.propertyPane.SelectedObject = null;
      }
    }

    public void UpdateWordSelectionInfo(WPSWordApplicationEventArgs args)
    {
      if (this.IsDisposed)
      {
        return;
      }
      var shapeObject = TryGetShapeWithWord();
      this.UpdateOfficeShapeInfo(shapeObject);
    }

    public void UpdatePowerPointSelectionInfo(WPSPowerPointApplicationEventArgs args)
    {
      if (this.IsDisposed)
      {
        return;
      }

      var shapeObject = TryGetShapeWithPowerPoint();
      this.UpdateOfficeShapeInfo(shapeObject);
    }

    private IOfficeShape TryGetShapeWithPowerPoint()
    {
      var app = WPSApplication.PwoerPointApplication;
      dynamic curSelectionObject = null;
      var selection = app.ActiveWindow.Selection;
      try
      {
        if (selection.ShapeRange.Count > 0)
        {
          curSelectionObject = selection.ShapeRange[1];
        }
      }
      catch
      {
        try
        {
          if (selection.SlideRange.Count > 0)
          {
            curSelectionObject = selection.SlideRange[1];
          }
        }
        catch
        {
          curSelectionObject = null;
        }
      }
      if (curSelectionObject != null)
      {
        try
        {
          return new PowerPointShapeObject(curSelectionObject);
        }
        catch { }
      }
      return null;
    }

    private IOfficeShape TryGetShapeWithWord()
    {
      return WordShapeObject.GetShapeObject(WPSApplication.WordApplication.Selection);
    }

    private IOfficeShape TryGetShapeWithExcel()
    {
      // Todo...
      return null;
    }

    private void FrmProperty_FormClosed(object sender, FormClosedEventArgs e)
    {
      WPPPlugin.PropertyForm = null;
      WPSApplication.OnWordSelectionChange -= WPSApplication_OnWordSelectionChange;
      WPSApplication.OnPowerPointSelectionChange -= WPSApplication_OnPowerPointSelectionChange;
      WPSApplication.OnExcelSelectionChange -= WPSApplication_OnExcelSelectionChange;
      WPSApplication = null;
    }
  }
}
